/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.jessyan.art.utils;



import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;


/**
 * ================================================
 * 权限请求工具类
 *
 * @see <a href="https://github.com/JessYanCoding/MVPArms/wiki#3.9">PermissionUtil wiki 官方文档</a>
 * Created by JessYan on 17/10/2016 10:09
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public final class PermissionUtil {
    public static final String TAG = "Permission";
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, TAG);

    private PermissionUtil() {
        throw new IllegalStateException("you can't instantiate me!");
    }

    public interface RequestPermission {

        /**
         * 权限请求成功
         */
        void onRequestPermissionSuccess();

        /**
         * 用户拒绝了权限请求, 权限请求失败, 但还可以继续请求该权限
         *
         * @param permissions 请求失败的权限名
         */
        void onRequestPermissionFailure(List<String> permissions);

        /**
         * 用户拒绝了权限请求并且用户选择了以后不再询问, 权限请求失败, 这时将不能继续请求该权限, 需要提示用户进入设置页面打开该权限
         *
         * @param permissions 请求失败的权限名
         */
        void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions);
    }

    // 权限配置请在 config.json 中申请
//    public static void requestPermission(final RequestPermission requestPermission, RxPermissions rxPermissions, RxErrorHandler errorHandler, String... permissions) {
//        if (permissions == null || permissions.length == 0) return;
//
//        List<String> needRequest = new ArrayList<>();
//        for (String permission : permissions) { //过滤调已经申请过的权限
//            if (!rxPermissions.isGranted(permission)) {
//                needRequest.add(permission);
//            }
//        }
//
//        if (needRequest.isEmpty()) {//全部权限都已经申请过，直接执行操作
//            requestPermission.onRequestPermissionSuccess();
//        } else {//没有申请过,则开始申请
//            rxPermissions
//                    .requestEach(needRequest.toArray(new String[needRequest.size()]))
//                    .buffer(permissions.length)
//                    .subscribe(new ErrorHandleSubscriber<List<Permission>>(errorHandler) {
//                        @Override
//                        public void onNext(@NotNull List<Permission> permissions) {
//                            List<String> failurePermissions = new ArrayList<>();
//                            List<String> askNeverAgainPermissions = new ArrayList<>();
//                            for (Permission p : permissions) {
//                                if (!p.granted) {
//                                    if (p.shouldShowRequestPermissionRationale) {
//                                        failurePermissions.add(p.name);
//                                    } else {
//                                        askNeverAgainPermissions.add(p.name);
//                                    }
//                                }
//                            }
//                            if (failurePermissions.size() > 0) {
//                                HiLog.warn(LABEL, "Request permissions failure");
//                                requestPermission.onRequestPermissionFailure(failurePermissions);
//                            }
//
//                            if (askNeverAgainPermissions.size() > 0){
//                                HiLog.warn(LABEL, "Request permissions failure with ask never again");
//                                requestPermission.onRequestPermissionFailureWithAskNeverAgain(askNeverAgainPermissions);
//                            }
//
//                            if (failurePermissions.size() == 0 && askNeverAgainPermissions.size() == 0){
//                                HiLog.warn(LABEL, "Request permissions success");
//                                requestPermission.onRequestPermissionSuccess();
//                            }
//                        }
//                    });
//        }
//    }
//
//    /**
//     * 请求摄像头权限
//     */
//    public static void launchCamera(RequestPermission requestPermission, RxPermissions rxPermissions, RxErrorHandler errorHandler) {
//        requestPermission(requestPermission, rxPermissions, errorHandler, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
//    }
//
//    /**
//     * 请求外部存储的权限
//     */
//    public static void externalStorage(RequestPermission requestPermission, RxPermissions rxPermissions, RxErrorHandler errorHandler) {
//        requestPermission(requestPermission, rxPermissions, errorHandler, Manifest.permission.WRITE_EXTERNAL_STORAGE);
//    }
//
//    /**
//     * 请求发送短信权限
//     */
//    public static void sendSms(RequestPermission requestPermission, RxPermissions rxPermissions, RxErrorHandler errorHandler) {
//        requestPermission(requestPermission, rxPermissions, errorHandler, Manifest.permission.SEND_SMS);
//    }
//
//    /**
//     * 请求打电话权限
//     */
//    public static void callPhone(RequestPermission requestPermission, RxPermissions rxPermissions, RxErrorHandler errorHandler) {
//        requestPermission(requestPermission, rxPermissions, errorHandler, Manifest.permission.CALL_PHONE);
//    }
//
//    /**
//     * 请求获取手机状态的权限
//     */
//    public static void readPhonestate(RequestPermission requestPermission, RxPermissions rxPermissions, RxErrorHandler errorHandler) {
//        requestPermission(requestPermission, rxPermissions, errorHandler, Manifest.permission.READ_PHONE_STATE);
//    }
}

